<template>
  <div class="app-container">
    <div class="filter-container">
      <selectServer @server-selected="handleSelectServer" />
      <el-date-picker v-model="searchForm.startDate" class="filter-item" type="date" style="width: 160px;" placeholder="开始时间"></el-date-picker>-
      <el-date-picker v-model="searchForm.endDate" class="filter-item" type="date" style="width: 160px;" placeholder="结束时间"></el-date-picker>
      <!-- <el-select v-model="searchForm.types" class="filter-item" style="width: 360px;" clearable multiple :loading="loading" placeholder="统计类型">
        <el-option v-for="(item, k) in types" :key="k" :label="item" :value="k"></el-option>
      </el-select> -->
      <el-button class="filter-item" type="primary" plain icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button :loading="downloadLoading" type="primary" icon="el-icon-download" class="filter-item" @click="handleDownload()">导出</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" :style="{height: '780px'}" class="mt-10" border height="780">
      <el-table-column label="日期" prop="date" width="150px" align="center"></el-table-column>
      <el-table-column label="活跃角色" prop="dau" width="150px" align="center"></el-table-column>
      <template v-if="types">
        <el-table-column
          v-for="column in columns"
          :key="column.prop"
          :label="column.label"
          :prop="column.prop"
          :width="column.width"
          :align="column.align || 'center'"
        />
      </template>
    </el-table>
  </div>
</template>

<script>
import { getActivityLossList } from '@/api/statis'
import checkPermission from '@/utils/permission'
import SelectServer from '@/components/SelectServer'

const types = {
  '24': '抽卡',
  '502': '海底',
  '531': '遗迹',
  '701': '海岛联赛',
  '601': '地心探勘',
  '223': '捕鱼'
}
const other_id = 24
export default {
  name: 'PlayActive',
  components: { SelectServer },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      list: [],
      serverList: [],
      mapList: [],
      columns: [],
      types: types,
      field_name: '角色等级',
      loading: false,
      listLoading: false,
      downloadLoading: false,
      searchForm: {
        startDate: new Date(Date.now() - 86400000 * 11),
        endDate: new Date(Date.now() - 86400000 * 1),
        types: [],
        sid: []
      }
    }
  },
  created() {
    // this.getList()
  },
  methods: {
    checkPermission,
    updateColumns() {
      const list = []
      for (const key in types) {
        if (key == other_id) {
          for (const cid in this.mapList) {
            list.push({
              prop: `type_${key}_${cid}`,
              label: types[key] + this.mapList[cid],
              width: '150px',
              align: 'center'
            })
          }
        } else {
          list.push({
            prop: `type_${key}`,
            label: types[key],
            width: '150px',
            align: 'center'
          })
        }
      }
      this.columns = list
    },
    handleFilter() {
      this.getList()
    },
    handleSelectServer(servers) {
      this.searchForm.servers = servers
    },
    handleChange(val) {
      // this.type = this.types[val]
    },
    getTypes() {
      const query = Object.assign({}, this.searchForm)
      if (query.types.length === 0) {
        query.types = Object.keys(types)
      }
      query.other_type = other_id
      return query
    },
    async getList() {
      this.listLoading = true
      const res = await getActivityLossList(this.getTypes())
      this.listLoading = false
      const mapList = res.data['map']
      this.mapList = mapList
      this.updateColumns()
      this.list = res.data.items
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['日期', '活跃角色']
        const filterVal = ['date', 'dau']
        const tHeader2 = this.columns.map(column => column.label)
        const filterVal2 = this.columns.map(column => column.prop)
        console.log(tHeader2)
        console.log(filterVal2)
        tHeader.push(...tHeader2)
        filterVal.push(...filterVal2)
        const data = this.formatJson(filterVal)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '玩家统计-整体统计'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        return v[j]
      }))
    }
  }
}
</script>
